package com.xuecheng.content.controller;

import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;

    // 课程预览
    @GetMapping("/coursePub/preview/{courseBaseId}")
    public ModelAndView preview(@PathVariable("courseBaseId") Long courseBaseId ){ //接受参数
        // 调用方法
         Map map= coursePubService.preview(courseBaseId);
         // 处理模板转发
        ModelAndView view = new ModelAndView("learing_article", map);
        // 返回结果
        return view;
    }

     // 课程发布
    @PostMapping("/coursePub/publish/{courseBaseId}")
    public ResponseResult kechengfabu(@PathVariable("courseBaseId") Long courseBaseId) throws IOException, TemplateException { // 接收参数
       //  调用方法 返回结果
       return coursePubService.kechengfabu(courseBaseId);
    }

    // 根据id查询已经发布的课程id
    @GetMapping("/coursePub/{id}")
    public ResponseResult fabuId(@PathVariable("id") Long id){ //接收参数
        //调用方法 返回结果
        CoursePub coursePub = coursePubService.getById(id);

        CoursePubVo vo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);

        return ResponseResult.okResult(vo);
    }


}
